package com.wlz.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.wlz.entity.ExportExcelLog;
import com.wlz.entity.ExportExcelUser;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wlz
 * @date 2022-09-23  4:02 下午
 */
public class PoiExcelUtil {

    public static void main(String[] args) {
        exportSheetUsers();
    }

    /**
     * excel多sheet导出
     */
    public static void exportSheetUsers() {

        //功能描述：把同一个表格多个sheet测试结果重新输出，
        Workbook workBook = null;
        try {

            // 创建参数对象（用来设定excel的sheet1内容等信息）
            ExportParams userExportParams = new ExportParams();
            // 设置sheet得名称
            userExportParams.setSheetName("用户表");
            // 设置sheet表头名称
            userExportParams.setTitle("用户列表");
            // 创建sheet1使用得map
            Map<String, Object> userExportMap = new HashMap<>();
            // title的参数为ExportParams类型，目前仅仅在ExportParams中设置了sheetName
            userExportMap.put("title", userExportParams);
            // 模版导出对应得实体类型
            userExportMap.put("entity", ExportExcelUser.class);
            //转成导出vo类型
            List<ExportExcelUser> users = new ArrayList<>();
            for (int i = 0; i< 10; i++) {
                ExportExcelUser exportExcelUser = new ExportExcelUser();
                exportExcelUser.setName("wlz+" + (i+1));
                exportExcelUser.setAge(i+1);
                users.add(exportExcelUser);
            }
            // sheet1中要填充得数据
            userExportMap.put("data", users);

            // 创建参数对象（用来设定excel的sheet2内容等信息）
            ExportParams logInfoExportParams = new ExportParams();
            logInfoExportParams.setTitle("日志列表");
            logInfoExportParams.setSheetName("日志表");
            // 创建sheet2使用的map
            Map<String, Object> logInfoExportMap = new HashMap<>();
            logInfoExportMap.put("title", logInfoExportParams);
            logInfoExportMap.put("entity", ExportExcelLog.class);
            //转成导出vo类型
            List<ExportExcelLog> logInfos = new ArrayList<>();
            for (int i = 0; i< 10; i++) {
                ExportExcelLog exportExcelLog = new ExportExcelLog();
                exportExcelLog.setCode((i+1));
                exportExcelLog.setUrl("wlz" + (i+1));
                logInfos.add(exportExcelLog);
            }
            // sheet2中要填充得数据
            logInfoExportMap.put("data", logInfos);

            // 将sheet1、sheet2使用得map进行包装
            List<Map<String, Object>> sheetsList = new ArrayList<>();
            //后续增加sheet组，则后面继续追加即可;
            sheetsList.add(userExportMap);
            sheetsList.add(logInfoExportMap);

            // 执行方法
            workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);


            String path =  "/Library/intest/java_workspace/data" + "/file/msn/2022/8/20220923130947_61961222_wlz.xlsx";
            File file = new File(path);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(file);
            workBook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workBook != null) {
                try {
                    //强行关流
                    workBook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
